package com.erp.servlet;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.erp.po.Customers;
import com.erp.po.Employees;
import com.erp.po.OrderDetail;
import com.erp.po.Orders;
import com.erp.po.Products;
import com.erp.service.CustomersServiceImpl;
import com.erp.service.EmployeesServiceImpl;
import com.erp.service.OrderDetailsServiceImpl;
import com.erp.service.OrderServiceImpl;
import com.erp.util.Page;
import com.erp.util.TimeHelper;
import com.google.gson.Gson;

/**
 * Servlet implementation class OrderServlet
 */
@WebServlet("/admin/orders/OrderServlet2")
public class OrderServlet2 extends HttpServlet {
	private static final long serialVersionUID = 1L;

	private OrderServiceImpl os;
	private EmployeesServiceImpl es;
	private CustomersServiceImpl cs;
	private OrderDetailsServiceImpl adsi;
	
	public OrderServlet2() {
		os = new OrderServiceImpl();
		es = new EmployeesServiceImpl();
		cs = new CustomersServiceImpl();
		adsi = new OrderDetailsServiceImpl();
	}

	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String op = request.getParameter("op");
		
		System.out.println("信息 : "+op+" ----- orderServlet ");
		
		if("findProducts".equals(op)) {
			// 使用ajax 查询product数据
			findProducts(request,response);
			
		} else if("findEmps".equals(op)) {
			// 使用ajax 查询employees数据
			findEmps(request,response);
			
		} else if("findCusts".equals(op)) {
			// 使用ajax 查询custs数据
			findCusts(request,response);
			
		} else if("showAllOrders".equals(op)) {
			// 使用ajax 查询所有订单[分页]
			showAllOrders(request,response);
			
		} else if("doAddOrder".equals(op)) {
			// 添加订单
			doAddOrder(request,response);
			
		} else if("searchOrderByCondition".equals(op)) {
			// 根据条件查询订单
			searchOrderByCondition(request,response);
		} else if("delOrderById".equals(op)) {
			// 删除order
			delOrderById(request,response);
		}
		
		
	}

	/**
	 * 删除order
	 */
	protected void delOrderById(HttpServletRequest request, HttpServletResponse response) 
			throws ServletException, IOException {

		String orderID = request.getParameter("orderId");

		System.out.println("信息 : "+orderID);
		
		int count = adsi.findDetailCountByOrderID(orderID);
		if(count > 0 ) {
			// 还有订单详情数据, 先删除订单-详情数据,在删除订单
			boolean f = adsi.removeDetailByOrderId(orderID);
			
			response.getWriter().println(f);
			// System.out.println(f+"== 先删详情,在删订单");
			
		} else if(count == 0) {
			// 没有订单详情数据, 直接删除订单
			boolean f = os.removeOrderById(orderID);
			response.getWriter().println(f);
		}
	}

	/**
	 * 根据条件查询订单
	 */
	protected void searchOrderByCondition(HttpServletRequest request, HttpServletResponse response) 
			throws ServletException, IOException {
		int currengPage = Integer.parseInt(request.getParameter("currengPage"));
		int pageSize = Integer.parseInt(request.getParameter("pageSize"));
		String orderID = request.getParameter("orderID");
		String orderDate = request.getParameter("orderDate");
		int customerID = Integer.parseInt(request.getParameter("customers"));
		int empID = Integer.parseInt(request.getParameter("employees"));
		
		// System.out.println(orderID+"  "+orderDate+"  "+"  "+customerID+"  "+employeeID);
		
		Page<Orders> page = os.findOrdersByCondition(new Page<Orders>(currengPage, pageSize), new Orders(orderID, orderDate, new Customers(customerID), new Employees(empID)));
		
		Gson gson = new Gson();
		response.getWriter().print(gson.toJson(page));
		
	}

	/**
	 * 添加订单
	 */
	protected void doAddOrder(HttpServletRequest request, HttpServletResponse response) 
			throws ServletException, IOException {
		
		String orderID = request.getParameter("orderID");
		String orderDate = request.getParameter("orderDate");
		int customerID = Integer.parseInt(request.getParameter("customers"));
		int empID = Integer.parseInt(request.getParameter("employees"));
		
		String[] proArr = request.getParameterValues("products");
		String[] quaArr = request.getParameterValues("quantity");
		String[] disArr = request.getParameterValues("discount");
		
//		System.out.println(orderID+"   "+orderDate+"   "+customerID+"   "+empID+"   ");
//		System.out.println(proArr.length);
		
		OrderDetail[] arr = new OrderDetail[proArr.length];
		for (int i = 0; i < proArr.length; i++) {
			arr[i] = new OrderDetail(Integer.parseInt(quaArr[i]),Double.parseDouble(disArr[i]),new Orders(orderID),new Products(Integer.parseInt(proArr[i])));
		}
		
		boolean f = adsi.addOrderDetails(new Orders(orderID, orderDate, new Customers(customerID), new Employees(empID)),arr);
		
		/*if(f) {
			System.out.println("添加成功");
		} else {
			System.out.println("添加失败");
		}
		*/
		response.getWriter().print(f);
		
		
	}

	/**
	 * 使用ajax 查询所有订单[分页]
	 */
	protected void showAllOrders(HttpServletRequest request, HttpServletResponse response) 
			throws ServletException, IOException {

		int currentPage = Integer.parseInt(request.getParameter("currentPage"));
		int pageSize = Integer.parseInt(request.getParameter("pageSize"));
		
		Page<Orders> page = os.findAllOrdersByPage(new Page<Orders>(currentPage, pageSize));
		
		// 将集合转为 json数据个格式的字符串
		Gson gson = new Gson();
		response.getWriter().print(gson.toJson(page));
		
	}

	/**
	 * 使用ajax 查询custs数据
	 */
	protected void findCusts(HttpServletRequest request, HttpServletResponse response) 
			throws ServletException, IOException {

		// 获取orderID
		String strTime = TimeHelper.getStrTime();
		
		List<Customers> list = cs.findAllCusts();
		
		Object[] arr = {strTime , list};
		
		// 将集合转为 json数据个格式的字符串
		Gson gson = new Gson();
		response.getWriter().print(gson.toJson(arr));
	}

	/**
	 * 使用ajax 查询employees数据
	 */
	protected void findEmps(HttpServletRequest request, HttpServletResponse response) 
			throws ServletException, IOException {

		List<Employees> list = es.findAllEmps();
		// 将集合转为 json数据个格式的字符串
		Gson gson = new Gson();
		response.getWriter().print(gson.toJson(list));
	}

	/**
	 * 使用ajax 查询product数据
	 */
	protected void findProducts(HttpServletRequest request, HttpServletResponse response) 
			throws ServletException, IOException {

		List<Products> list = os.findProducts();
		
		// 将集合转为 json数据个格式的字符串
		Gson gson = new Gson();
		response.getWriter().print(gson.toJson(list));
		
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doGet(request, response);
	}

}
